<template>
  <a-modal
    :title="title"
    :width="640"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          v-show="!addable"
          label="id"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            v-decorator="['id']"
            :disabled="editable || showable"
          />
        </a-form-item>
        <a-form-item
          label="上级部门"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-tree-select
            :disabled="showable"
            showSearch
            placeholder="选择上级部门"
            allowClear
            :treeData="treeData"
            treeDefaultExpandAll
            treeNodeFilterProp="title"
            v-decorator="['parentId', {rules: [{required: true, message: '请选择上级部门'}],initialValue: '0'}]"
          />
        </a-form-item>
        <a-form-item
          label="部门名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            :disabled="showable"
            placeholder="请输入部门名称"
            v-decorator="['name', {rules: [{required: true, message: '请输入部门名称'}]}]" />
        </a-form-item>
        <a-form-item
          label="排序"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input-number
            :disabled="showable"
            placeholder="请输入排序"
            v-decorator="['orderNum']" />
        </a-form-item>
      </a-form>
    </a-spin>

    <template slot="footer">
      <div v-show="editable|| addable">
        <a-button key="cancel" @click="handleCancel">取消</a-button>
        <a-button key="forward" :loading="confirmLoading" type="primary" @click="handleOk">保存</a-button>
      </div>
    </template>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { addObj, putObj, getObj, fetchTree,EditTree } from '@api/sys/dept'
import { treeDataTranslate } from '@/utils/util'
import { FormMixin } from '@/mixins/FormMixin'

export default {
  name: 'DeptAddOrUpdate',
  mixins: [FormMixin],
  data () {
    return {
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this),
      treeData: []
    }
  },
  methods: {
    edit (id, type) {
      this.visible = true
      const { form: { setFieldsValue, resetFields } } = this
      if (['edit', 'show'].includes(type)) {
        EditTree(id).then(res => {
          this.treeData = treeDataTranslate(res.data, 'id', 'name')
        }).catch((err) => {
          this.$message.error(err.msg)
        })
        this.confirmLoading = true
        getObj(id).then(res => {
          const record = res.data
          record.parentId = String(record.parentId)
          this.confirmLoading = false
          this.$nextTick(() => {
            setFieldsValue(pick(record, ['id', 'name', 'parentId','orderNum']))
          })
        })
      } else {
        fetchTree().then(res => {
          this.treeData = treeDataTranslate(res.data, 'id', 'name')
        })
        resetFields()
      }
    },
    handleOk () {
      this.form.validateFields(async (err, values) => {
        if (!err) {
          if (this.type === 'add') {
            await addObj(values)
          } else if (this.type === 'edit') {
            await putObj(values)
          }
          this.confirmLoading = true
          setTimeout(() => {
            this.confirmLoading = false
            this.$emit('ok')
            this.visible = false
          }, 1500)
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
